/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.repository;

import me.zhengjie.dto.sqlServer.Storage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
* @website https://eladmin.vip
* @author zhangYY
* @date 2024-01-12
**/
public interface StorageRepository extends JpaRepository<Storage, Integer>, JpaSpecificationExecutor<Storage> {
    /**
    * 根据 StorageCode 查询
    * @param storage_code /
    * @return /
    */
    Storage findByStorageCode(String storage_code);

    @Query("select s.storageCode from Storage s where s.id =?1")
    String findCodeById(Integer StorageId);

    @Query("select s.storageCode from Storage s where s.palletId =?1")
    String findCodeByPalletId(Integer palletId);

    @Query("select s from Storage s where s.zoneCode=?1 and s.locationInventoryStatus=0 and s.status=1")
    List<Storage> findStorageByAreaIdAndStatus(String areaCode);

    List<Storage> findStoragesByZoneCode(String area);

    List<Storage> findStoragesByRowAndZoneCodeAndLayerOrderByColumnDesc(int row, String area, int layer);

    @Modifying
    @Query("update Storage s set s.status=0 where s.id=?1")
    void updateStorageStatusById(Integer storageId);

    @Modifying
    @Query("update Storage s set s.locationInventoryStatus=0 , s.status=1, s.palletId=null where s.storageCode=?1")
    void updateStorageInventoryStatus(String storageCode);

    @Modifying
    @Query("update Storage s set s.status=1 where s.storageCode=?1")
    int updateStorageStatus(String storageCode);

    @Query("select s from Storage s where s.status=1")
    List<Storage> findAllOpenStorage();

    @Modifying
    @Query("update Storage s set s.locationInventoryStatus=1 , s.palletId=?1 where s.storageCode=?2")
    int updatePalletCode(Integer palletId,String storageCode);

    @Modifying
    @Query("update Storage s set s.status=?1 where s.id=?2")
    int changeStorageStatusById(Integer status, Integer storageId);
}